using System;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using UnityEngine;

namespace Gameplay.PVE.Skill
{
    public class SpecialMover : SkillInitiativeMover
    {
        private int state = 0;//0跳上车 1移动 2 技能 3 技能后摇 4 回来 5 跳跃
        private float stateStartTime;
        private float jumpTime;
        private float jumpDuration = 0.8f;
        private float jumpDistance = 5.4f;
        private float jumpSpeed = 1;
        private float speed = 0;
        private float accelerate = 30;
        private float maxSpeed = 15;
        private float backSpeed;

        private Vector3 startPosition;
        private float skillStartTime = 0.05f;
        public override void Start(Vector3 startPosition,Vector3 endPosition)
        {
            base.Start(startPosition,endPosition);
            unit.Play("SkillStart");
            unit.PlaySkill("SkillStart");

            unit.Data.moveState = 3;
            stateStartTime = TimeManager.logicTime;
            var distance = Vector3.Distance(endPosition,unit.Data.position);
            this.startPosition = unit.Data.position;
            //匀加速+匀速
            /*float accelerateTime = maxSpeed / accelerate;
            float accelerateDistance = accelerate * accelerateTime * accelerateTime / 2;
            float uniformSpeedDistance = distance - accelerateDistance;
            float uniformSpeedTime = uniformSpeedDistance / maxSpeed;
            float totalTime = accelerateTime;
            if (uniformSpeedTime > 0)
            {
                totalTime += uniformSpeedTime;
            }*/

            
            
            speed = 15;
            jumpTime = -1;
            if (distance > jumpDistance)
            {
                jumpTime = jumpDuration; //(distance - jumpDistance) / speed;
                jumpSpeed = distance / jumpTime;
            }

            accelerate = 0;
           
            maxSpeed = 20;
            state = 0;
            backSpeed = distance / 0.33f;
            /*if (backSpeed > 10)
            {
                backSpeed = 10;
            }*/
        }
        
        public override void Recover()
        {
            base.Recover();
            //unit.Play("SkillEnd");
        }
        
        public override void Update()
        {
            base.Update();

            if (state == 0)
            {
                MoveToPosition(endPosition);
                if (TimeManager.logicTime - stateStartTime >= skillStartTime)
                {
                    stateStartTime = TimeManager.logicTime;
                    state = 1;
                    if (jumpTime < 0)
                    {
                        unit.Play("SkillRun");
                    }
                }
            }
            else if (state == 1 || state == 5)
            {
                if (MoveToPosition(endPosition))
                {
                    state = 2;
                    skill.DoStartCast();
                    moveFinished = true;
                    unit.Play("SkillLoop");
                    stateStartTime = TimeManager.logicTime;
                    PveEffectManager.Instance.PlayEffect(4001,endPosition,unit.Data.forward,5f);
                }
                else if (state != 5 && jumpTime >= 0)
                {
                    state = 5;
                    speed = jumpSpeed;
                    unit.Play("SkillJump");
                }
            }
            else if (state == 2)
            {
                if (TimeManager.logicTime - stateStartTime < skill.config.attack_cast_time)
                {
                    skill.DoCastEvent();
                    //unit.Data.position += unit.Data.moveOffset;
                }
                else
                {
                    state = 3;
                    unit.Play("SkillLoopEnd");
                    stateStartTime = TimeManager.logicTime;
                }
            }
            else if (state == 3)
            {
                if (TimeManager.logicTime - stateStartTime >= 0.5f)
                {
                    speed = backSpeed;
                    maxSpeed = backSpeed;
                    state = 4;
                    unit.Play("SkillEnd");
                    unit.PlaySkill("SkillEnd");
                    stateStartTime = TimeManager.logicTime;
                }
            }
            else if (state == 4)
            {
                /*if (TimeManager.logicTime - stateStartTime < 0.33f)
                {
                    MoveToPosition(startPosition);
                }
                if (TimeManager.logicTime - stateStartTime > 1.167f)
                {
                    backFinished = true;
                }*/
                if (MoveToPosition(startPosition))
                {
                    backFinished = true;
                }
            }
        }

        private bool MoveToPosition(Vector3 targetPosition)
        {
            var diff = targetPosition - unit.Data.position;
            this.speed += accelerate * TimeManager.LogicDeltaTime;
            if (this.speed > maxSpeed)
            {
                this.speed = maxSpeed;
            }
            var speed = this.speed * TimeManager.LogicDeltaTime;
            if (diff.sqrMagnitude <= speed * speed)
            {
                unit.Data.position = targetPosition;
                return true;
            }
            else
            {
                var forward =  diff.normalized;
                unit.Data.position += forward * speed;
                //unit.Data.SetTargetForward(forward);
                return false;
            }
        }

        public override void ForceStop()
        {
            base.ForceStop();
            unit.Play("ForceStop");
        }
    }
}